<template>
  <el-dialog
    v-model="dialogVisibled"
    :title="dialogTitle"
    width="500"
    :draggable="true"
    :before-close="handleClose"
    @open="handleDialogOpen"
  >
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="modelData"
      status-icon
      :rules="rules"
      label-width="auto"
      class="demo-ruleForm"
    >
      <el-form-item label="角色名称" prop="RoleName">
        <el-input v-model="modelData.RoleName" maxlength="20" />
      </el-form-item>
      <el-form-item label="所属公司" prop="CorporationKey">
        <el-select
          @change="corporationClick"
          v-model="modelData.CorporationKey"
          placeholder="请选择公司"
          :disabled="modelData.RoleId === 0 ? false : true"
        >
          <el-option
            v-for="item in corporationList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            :disabled="item.disabled"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属系统" prop="SystemKey">
        <el-select
          @change="systemClick"
          v-model="modelData.SystemKey"
          placeholder="请选择系统"
          :disabled="modelData.RoleId === 0 ? false : true"
        >
          <el-option
            v-for="item in systemList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            :disabled="item.disabled"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上级角色" prop="Pid">
        <el-tree-select
          placeholder="请选择上级角色,不选择为顶级角色"
          v-model="modelData.Pid"
          :data="roleList"
          check-strictly
          :default-expanded-keys="[
            modelData.Pid === undefined ? 0 : modelData.Pid,
          ]"
          :render-after-expand="false"
        >
        </el-tree-select>
      </el-form-item>
      <el-form-item label="是否启用" prop="IsOpen">
        <el-switch
          v-model="modelData.IsOpen"
          class="ml-2"
          width="60"
          inline-prompt
          active-text="启用"
          inactive-text="不启用"
        />
      </el-form-item>
      <div style="justify-content: right; align-items: right; display: flex">
        <el-button type="success" icon="Check" @click="submitForm(ruleFormRef)">
          提交
        </el-button>
        <!-- <el-button
          type="warning"
          icon="RefreshRight"
          @click="resetForm(ruleFormRef)"
          >重置</el-button
        > -->
        <el-button type="danger" icon="Close" @click="handleClose"
          >关闭</el-button
        >
      </div>
    </el-form>
  </el-dialog>
</template>
    <script  lang="ts">
import {
  ref,
  reactive,
  onMounted,
  defineComponent,
  PropType,
  computed,
} from "vue";
import { ElMessage, FormInstance, FormRules } from "element-plus";
import { getRoleTree, insert, update } from "@/api/role";
import { selectOutPut } from "@/model/public/selectModel";
import { SysSystem } from "@/model/system/system";
import { getAllCorporationSelect } from "@/api/corporation";
import { SysRole } from "@/model/role/role";
import { getAllSystemSelect } from "@/api/system";
import { treeOutPut } from "@/model/public/treeModel";
export default defineComponent({
  props: {
    isVisibles: {
      type: Boolean,
    },
    dialogTitle: { type: String },
    modelData: {
      type: Object as PropType<SysRole>,
      required: true,
    },
  },

  setup(props, context) {
    //公司数据
    const corporationList = ref<selectOutPut[]>([]);
    //系统数据
    const systemList = ref<selectOutPut[]>([]);
    //角色数据
    const roleList = ref<treeOutPut[]>([]);
    //公司key
    const corporationKey = ref<string>("");
    //系统key
    const systemKey = ref<string>("");
    //角色key
    const roleKey = ref<number | undefined | string>();
    //默认展开
    const defaultExpandedKeys = ref<string[]>([]);

    //初始加载dom
    onMounted(() => {
      getCorporation();
      //getSystemSelectMsg();
      //getRoleTreeMsg();
    });

    //打开时加载
    const handleDialogOpen = function () {
      corporationKey.value = props.modelData.CorporationKey;
      systemKey.value = props.modelData.SystemKey;
      roleKey.value = props.modelData.Pid;
      // defaultExpandedKeys.value.push(roleKey.value);
      systemClick(props.modelData.SystemKey);
      getSystemSelectMsg(props.modelData.CorporationKey);
    };

    //获取下拉框中的公司数据
    const getCorporation = function () {
      getAllCorporationSelect().then(({ data, code, msg }) => {
        if (code == 200) {
          corporationList.value = data;
        }
      });
    };

    //获取下拉框中的系统数据
    const getSystemSelectMsg = function (corporationKey: string) {
      getAllSystemSelect({ corporationKey: corporationKey }).then(
        ({ data, code, msg }) => {
          if (code == 200) {
            systemList.value = data;
          }
        }
      );
    };

    //公司点击事件
    const corporationClick = function (value: string) {
      if (value == corporationKey.value) {
        props.modelData.SystemKey = systemKey.value;
        props.modelData.Pid = roleKey.value;
        systemClick(systemKey.value);
      } else {
        props.modelData.SystemKey = "";
        props.modelData.Pid = undefined;
      }
      roleList.value = [];
      corporationKey.value = value;
      getSystemSelectMsg(value);
    };

    //系统点击事件
    const systemClick = function (value: string) {
      roleList.value = [];
      props.modelData.Pid = undefined;
      if (value == systemKey.value) props.modelData.Pid = roleKey.value;
      else props.modelData.RoleId = 0;
      if (value != "") getRoleTreeMsg(corporationKey.value, value);
    };

    //获取角色数据
    const getRoleTreeMsg = function (
      corporationKey: string,
      systemKey: string
    ) {
      getRoleTree({
        corporationKey: corporationKey,
        systemKey: systemKey,
      }).then(({ data, code, msg }) => {
        if (code == 200) {
          roleList.value = data;
        }
      });
    };

    //验证规则
    const ruleFormRef = ref<FormInstance>();
    const rules = reactive<FormRules<typeof props.modelData>>({
      RoleName: [
        {
          required: true,
          message: "请填写角色名称!",
          trigger: "blur",
        },
      ],
      SystemKey: [
        {
          required: true,
          message: "请填写系统编码!",
          trigger: "blur",
        },
      ],
      CorporationKey: [
        {
          required: true,
          message: "请选择所属公司!",
          trigger: "blur",
        },
      ],
    });

    //提交
    const submitForm = (formEl: FormInstance | undefined) => {
      if (!formEl) return;
      formEl.validate((valid) => {
        if (valid) {
          if (props.modelData.RoleId == 0) {
            insert(props.modelData).then(({ code, msg }) => {
              if (code == 200) {
                handleClose();
                ElMessage({
                  message: msg,
                  type: "success",
                });
              }
            });
          } else {
            update(props.modelData).then(({ code, msg }) => {
              if (code == 200) {
                handleClose();
                ElMessage({
                  message: msg,
                  type: "success",
                });
              }
            });
          }
        }
      });
    };

    //重置
    const resetForm = (formEl: FormInstance | undefined) => {
      if (!formEl) return;
      formEl.resetFields();
    };

    //是否显示dialog
    const dialogVisibled = computed(() => {
      return props.isVisibles;
    });

    //关闭事件
    const handleClose = () => {
      context.emit("closeBnt");
      resetForm(ruleFormRef.value);
      if (corporationKey.value != "" && systemKey.value != "")
        getRoleTreeMsg(corporationKey.value, systemKey.value);
      //  done();
    };

    return {
      handleClose,
      dialogVisibled,
      ruleFormRef,
      submitForm,
      rules,
      resetForm,
      corporationList,
      systemList,
      roleList,
      corporationClick,
      systemClick,
      corporationKey,
      systemKey,
      handleDialogOpen,
      defaultExpandedKeys,
    };
  },
  components: {},
});
</script>
    
    <style>
</style>